# -*- coding: utf-8 -*-
# @Time    : 2024/6/6 上午11:58
# @Author  : zhh
import os

import yaml

tokenPath = "E:\\PyCharm\\PycharmProjects\\WebApiTest\\data\\data.yml"
filePath = "E:\\PyCharm\\PycharmProjects\\WebApiTest\\data\\"
class YamlUtils():

    # 读取yaml文件
    @staticmethod
    def read_yaml(key):
        with open(tokenPath, 'r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value[key]

    # 写入yaml
    @staticmethod
    def write_yaml(data):
        with open(tokenPath, mode='a', encoding='utf-8') as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    # 清除文件内容
    # 如果希望直接通过类.方法调用 可以添加@classmethod注解  否则要创建实例对象来调用
    @classmethod
    def clear_yaml(self):
        with open(tokenPath, mode='a', encoding='utf-8') as f:
            f.truncate()

    @staticmethod
    def read_testcase_yaml(yaml_name):
        with open(filePath +yaml_name, 'r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value


# # 创建 YamlUtils 类的实例
# yaml_utils = YamlUtils()
#
# # 调用 write_yaml 方法
# yaml_utils.write_yaml(data={"csrf_token": 1111})
